package cn.itcast.bos.web.action.subarea;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.subarea.SubAreaService;
import cn.itcast.bos.web.action.base.BaseAction;

@Controller
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class SubAreaAction extends BaseAction<SubArea> {
	@Autowired
	// 按类型注入
	private SubAreaService subAreaService;

	// 文件的一件导入
	private File file;
	private String fileFileName;

	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	// 文件导入后页面重新加载 没有数据向页面传送 没有页面的跳转
	@Action(value = "subArea_import")
	public String importFile() throws FileNotFoundException, IOException,
			InvalidFormatException {
		List<SubArea> list = new ArrayList<SubArea>();

		// 判断文件是否为空
		if (file != null) {
			Sheet sheet = null;
			// 判断文件的类型
			if (fileFileName.endsWith("xls")) {
				HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(file));
				sheet = hwb.getSheetAt(0);

			} else if (fileFileName.endsWith("xlsx")) {
				XSSFWorkbook xwb = new XSSFWorkbook(file);
				sheet = xwb.getSheetAt(0);
			}
			// 循环遍历rows
			for (Row row : sheet) {

				// 去掉第一行的表头 和 第一个单元格是空的行
				if (row.getRowNum() == 0) {
					continue;
				}
				if (row.getCell(0) == null
						|| StringUtils.isBlank(row.getCell(0)
								.getStringCellValue())) {
					continue;
				}
		        		        
				SubArea subArea = new SubArea();
				subArea.setId(row.getCell(0).getStringCellValue());
		          row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
				subArea.setFixedArea(subAreaService.findFixedAreaById(row
						.getCell(1).getStringCellValue()));
				subArea.setArea(subAreaService.findAreaById(row.getCell(2)
						.getStringCellValue()));
				subArea.setKeyWords(row.getCell(3).getStringCellValue());
			
				subArea.setStartNum(row.getCell(4).getStringCellValue());
				
				subArea.setEndNum(row.getCell(5).getStringCellValue());
			
				subArea.setSingle(row.getCell(6).getStringCellValue().charAt(0));
				subArea.setAssistKeyWords(row.getCell(7).getStringCellValue());				
				//一定要记住最后把数据要添加到集合中
				list.add(subArea);
			}

		}
		subAreaService.saveBath(list);
		return NONE;

	}
}
